/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.*;
import util.WebConst;
/**
 *
 * @author trietto
 */
public class CategoryDAO {
    private PreparedStatement pStmtSelectCount = null;
    private PreparedStatement pStmtSelectAll = null;
    private PreparedStatement pStmtSelectAllPerPage = null;
    private PreparedStatement pStmtSelect = null;
    private PreparedStatement pStmtInsert = null;
    private PreparedStatement pStmtDelete = null;
    private PreparedStatement pStmtUpdate = null;
    private static CategoryDAO categoryDAO = null;

    public static CategoryDAO singleton() {
        if (categoryDAO == null) {
            categoryDAO = new CategoryDAO();
        }
        return categoryDAO;
    }

    public CategoryDAO() {
        try {
            pStmtSelectCount = DBConnection.getConnection().prepareStatement("SELECT Count(CategoryId) FROM Categories");
            pStmtSelectAll = DBConnection.getConnection().prepareStatement("SELECT * FROM Categories");
            pStmtSelectAllPerPage = DBConnection.getConnection().prepareStatement("SELECT * FROM Categories LIMIT ?,?");
            pStmtSelect = DBConnection.getConnection().prepareStatement("SELECT * FROM Categories WHERE CategoryId = ?");
            pStmtInsert = DBConnection.getConnection().prepareStatement("INSERT INTO Categories(Name) VALUES(?)");
            pStmtDelete = DBConnection.getConnection().prepareStatement("DELETE FROM Categories WHERE CategoryId = ?");
            pStmtUpdate = DBConnection.getConnection().prepareStatement("UPDATE Categories SET Name = ? WHERE CategoryId = ?");
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int recordCount(){
        int totalRecord = 0;
        try {
            ResultSet rs = pStmtSelectCount.executeQuery();
            while (rs.next()) {
                totalRecord = rs.getInt(1);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return totalRecord;
    }

    public ArrayList<Category> selectAll() {
        ArrayList<Category> lstCategories = new ArrayList<Category>();
        try {
            ResultSet rs = pStmtSelectAll.executeQuery();
            while (rs.next()) {
                lstCategories.add(new Category(rs.getInt(1), rs.getString(2)));
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lstCategories;
    }

    public ArrayList<Category> selectAll(int page) {
        ArrayList<Category> lstCategories = new ArrayList<Category>();
        int offset = (page-1) * WebConst.ROW_PER_PAGE;
        try {
            pStmtSelectAllPerPage.setInt(1, offset);
            pStmtSelectAllPerPage.setInt(2, WebConst.ROW_PER_PAGE);
            ResultSet rs = pStmtSelectAllPerPage.executeQuery();
            while (rs.next()) {
                lstCategories.add(new Category(rs.getInt(1), rs.getString(2)));
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lstCategories;
    }

    public Category select(int categoryId) {
        Category category = null;
        try {
            pStmtSelect.setInt(1, categoryId);
            ResultSet rs = pStmtSelect.executeQuery();
            while (rs.next()) {
                category = new Category(rs.getInt(1), rs.getString(2));
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return category;
    }

    public String insert(Category category) {
        String result = "";
        boolean bRet = false;
        try {
            pStmtInsert.setString(1, category.getName());
            pStmtInsert.execute();
            bRet = true;
        } catch (SQLException ex) {           
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = bRet ? "Successed" : "Failed";
        return result;
    }

    public String delete(int categoryId) {
        String result = "";
        int numOfRows = 0;
        try {
            pStmtDelete.setInt(1, categoryId);
            numOfRows = pStmtDelete.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows>0 ? "Successed" : "Failed";

        return result;
    }

    public String update(Category category) {
               String result = "";
        int numOfRows = 0;
        try {
            pStmtUpdate.setString(1, category.getName());
            pStmtUpdate.setInt(2, category.getCategoryId());
            numOfRows = pStmtUpdate.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows>0 ? "Successed" : "Failed";

        return result;
    }
}
